'use strict';
const semver = require("semver")
const colors = require("colors")

const {isObject} = require("@xykj-cli-dev/utils")

const LOWEST_NODE_VERSION = "12.0.0"
class Command{
    constructor(argv) {
        if(!argv){
            throw new Error("参数不能为空")
        }
        if(!Array.isArray(argv)){
            throw new Error("参数要是数组")
        }
        if(argv.length < 1){
            throw new Error("参数列表为空")
        }
        this._argv = argv
        let runner = new Promise((resolve,reject)=>{
            let chain = Promise.resolve()
            chain = chain.then(()=>this.checkNodeVersion())
            chain = chain.then(()=>this.initArgs())
            chain = chain.then(()=>this.init())
            chain = chain.then(()=>this.exec())
            chain.catch(err=>{
                console.log(err.message)
            })
        })
    }

    initArgs(){
        this._cmd = this._argv[this._argv.length-1]
        this._argv = this._argv.slice(0,this._argv.length-1)
    }

    checkNodeVersion(){
        // 获取当前版本号
        const currentVersion = process.version
        console.log(!semver.gte(currentVersion,LOWEST_NODE_VERSION))
        if(!semver.gte(currentVersion,LOWEST_NODE_VERSION)){
            throw new Error(console.log(('xykj需要node版本在'+LOWEST_NODE_VERSION+'以上').red))
        }
    }
    init(){
        throw new Error('init必须实现')
    }
    exec(){
        throw new Error('exec必须实现')
    }
}

module.exports = Command;

